<?php
/**
 * User:于小涛
 * Date:2020/2/17
 * Time:15:37
 */
namespace app\admin\controller\rbac;

use app\admin\BaseController;
use app\admin\model\rbac\Auth as AuthModel;
use app\admin\business\rbac\Auth as AuthBusiness;
use think\App;
use think\facade\View;

class Auth extends BaseController {

    private $obj_auth_business=null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->obj_auth_business = new AuthBusiness();
    }

    public function getList(){
        return view();
    }

    public function getListData(){
        $arr_list=$this->obj_auth_business->getAuthListStruct();
        return output_data($arr_list);
    }

    public function add(){
        if($this->request->param('name')){
            //计算level
            $int_level=$this->obj_auth_business->getLevelByPid($this->request->param('pid'));
            $arr_data_add=array(
                'name'=>$this->request->param('name'),
                'pid'=>$this->request->param('pid'),
                'url'=>$this->request->param('url'),
                'order_num'=>$this->request->param('order_num'),
                'is_lock'=>!empty($this->request->param('is_lock'))?1:2,
                'icon'=>$this->request->param('icon')??'',
                'level'=>$int_level,
            );
            $obj_auth_model=new AuthModel();
            $int_id=$obj_auth_model->add($arr_data_add);
            if($int_id){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            $arr_admin_info=session('admin');
            switch ($arr_admin_info['type']){
                case 0:
                    $arr_auth=null;
                    break;
                case 1:
                    $arr_auth=array();
                    break;
                case 2:
                    $arr_auth=$arr_admin_info['auth_ids'];
                    break;
            }
            $str_option_html=$this->obj_auth_business->getMenuHtmlOption(0,$arr_auth);
            View::assign('str_option_html',$str_option_html);
            return View::fetch();
        }
    }

    public function edit(){
        $obj_auth_model=new AuthModel();
        if($this->request->param('name')){
            //计算level
            $int_level=$this->obj_auth_business->getLevelByPid($this->request->param('pid'));
            $arr_data_edit=array(
                'name'=>$this->request->param('name'),
                'pid'=>$this->request->param('pid'),
                'url'=>$this->request->param('url'),
                'order_num'=>$this->request->param('order_num'),
                'is_lock'=>!empty($this->request->param('is_lock'))?1:2,
                'icon'=>$this->request->param('icon')??'',
                'level'=>$int_level,
            );
            $int_num=$obj_auth_model->edit($this->request->param('id'),$arr_data_edit);
            if($int_num){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            $arr_info=$obj_auth_model->find($this->request->param('id'));
            View::assign('arr_data_info',$arr_info);
            $arr_admin_info=session('admin');
            switch ($arr_admin_info['type']){
                case 0:
                    $arr_auth=null;
                    break;
                case 1:
                    $arr_auth=array();
                    break;
                case 2:
                    $arr_auth=$arr_admin_info['auth_ids'];
                    break;
            }
            $str_option_html=$this->obj_auth_business->getMenuHtmlOption(0,$arr_auth,$arr_info['pid'],array($this->request->param('id')));
            View::assign('str_option_html',$str_option_html);
            return view();
        }
    }

    public function lock(){
        $obj_auth_model=new AuthModel();
        $arr_data_edit=array(
            'is_lock'=>$this->request->param('is_lock')??2,
        );
        $int_num=$obj_auth_model->edit($this->request->param('id'),$arr_data_edit);
        if($int_num){
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }

    public function del(){
        $obj_auth_model=new AuthModel();
        $arr_data_edit=array(
            'is_del'=>2,
        );
        $int_num=$obj_auth_model->edit($this->request->param('id'),$arr_data_edit);
        if($int_num){
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }

    public function authTree(){
        $arr_list_data=$this->obj_auth_business->getAuthTree($this->request->param('role_id'));
        return output_data($arr_list_data);
    }
}